
package com.example.filetransfer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MyProgressBar extends View implements OnTouchListener {
    private static final String TAG = "MyProgressBar";
    private Paint paint;
    protected int outlineColor;
    protected int ringColor;
    protected int centerColor;
    protected int ringWidth;
    protected boolean indeterminate;
    private int textColor;
    private String text;

    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        parseAttributes(context.obtainStyledAttributes(attrs,
                R.styleable.MyProgressBar));
        setOnTouchListener(this);
    }

    private volatile boolean mTouching = false;
    private volatile boolean mLock = false;

    private void parseAttributes(TypedArray typedArray) {
        this.outlineColor = (int) typedArray.getDimension(R.styleable.MyProgressBar_outlineColor,
                getColor(android.R.color.darker_gray));
        this.ringColor = (int) typedArray.getDimension(R.styleable.MyProgressBar_ringColor,
                getColor(android.R.color.holo_green_light));
        this.centerColor = (int) typedArray.getDimension(R.styleable.MyProgressBar_centerColor,
                getColor(android.R.color.holo_blue_dark));
        this.textColor = (int) typedArray.getDimension(R.styleable.MyProgressBar_textColor,
                getColor(android.R.color.primary_text_dark));
        if (typedArray.hasValue(R.styleable.MyProgressBar_text)) {
            text = typedArray.getString(R.styleable.MyProgressBar_text);
        }
        this.paint = new Paint();
        this.paint.setAntiAlias(true);
        this.ringWidth = 4;
        // this.circleScale = circleScale;
        this.indeterminate = false;
        typedArray.recycle();
    }

    private int getColor(int resourceId) {
        return getResources().getColor(resourceId);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int layout_width, int layout_height, int oldw, int oldh) {
        super.onSizeChanged(layout_width, layout_height, oldw, oldh);
        int minValue = Math.min(layout_width, layout_height);

        // Calc the Offset if needed
        int xOffset = layout_width - minValue;
        int yOffset = layout_height - minValue;

        // Add the offset
        int paddingTop = this.getPaddingTop() + (yOffset / 2);
        int paddingBottom = this.getPaddingBottom() + (yOffset / 2);
        int paddingLeft = this.getPaddingLeft() + (xOffset / 2);
        int paddingRight = this.getPaddingRight() + (xOffset / 2);

        circleBounds = new RectF(paddingLeft + ringWidth,
                paddingTop + ringWidth,
                this.getLayoutParams().width - paddingRight - ringWidth,
                this.getLayoutParams().height - paddingBottom - ringWidth);
    }

    private RectF circleBounds = null;
    private int currentArc = 0;

    private int currentPorgress = 0;

    public void setProgress(int progress) {
        if (mTouching) {
            Log.d(TAG, "touching !!");
            return;
        }
        this.currentPorgress = progress;
        currentArc = (int) (360 * (currentPorgress/100f));
        if (currentArc >= 359) {
            currentArc = 360;
        }
        postInvalidate();
        if (progress < 100 && !mLock) {
            mLock = true;
        }
        if (progress >= 100 && mLock) {
            mLock = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float outerRadius = (circleBounds.width() / 2) - (ringWidth / 2);
        float innerRadius = outerRadius * (currentPorgress/100f);

        if (mTouching) {
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(ringColor);
            canvas.drawCircle(circleBounds.centerX(), circleBounds.centerY(), outerRadius, paint);

            // Inner circle
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.RED);
            canvas.drawCircle(circleBounds.centerX(), circleBounds.centerY(), innerRadius, paint);

            return;
        }

        // Outline Circle
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(ringColor);
        canvas.drawCircle(circleBounds.centerX(), circleBounds.centerY(), outerRadius, paint);

        // Inner circle
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(centerColor);
        canvas.drawCircle(circleBounds.centerX(), circleBounds.centerY(), innerRadius, paint);

        paint.setColor(outlineColor);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawArc(circleBounds, currentArc - 30, currentArc, false, paint);

        if (currentPorgress > 0) {
            text = currentPorgress + "%";
        }
        paint.setColor(textColor);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(1);
        paint.setTextSize(50);
        if (!TextUtils.isEmpty(text)) {
            float offset = paint.measureText(text) / 2;
            canvas.drawText(text, circleBounds.width() / 2 - offset,
                    circleBounds.height() / 2 + (25),
                    paint);
        }
    }
    public void reset() {
        mLock = false;
        currentPorgress = 0;
        postInvalidate();
    }

    private OnClickListener clickListener;

    public void setText(String text) {
        this.text = text;
        postInvalidate();
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        clickListener = l;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mLock) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!mTouching) {
                    mTouching = true;
                    postInvalidate();
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mTouching) {
                    mTouching = false;
                    postInvalidate();
                    if (clickListener != null) {
                        clickListener.onClick(this);
                    }
                    return true;
                }
                break;
            default:
                break;
        }
        return false;
    }
}
